#pragma comment(linker, "/STACK:10000000")
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <set>
#include <map>
#include <cstdio>
#include <cassert>
#include <string>
#include <cstring>
#define ldb long double
#define LL long long
#define fi first
#define se second
#define fill(a, c) memset(a, c, sizeof(a))
#define sqr(a) ((a) * (a))
#define nextLine() {int c = 0; while((c = getchar()) != 10 && c != EOF);}
#define getBit(mask, k) (((mask) / pw[k]) % pw[1])
#define setBit(mask, k, l) (((mask) / pw[k + 1] * pw[1] + (l)) * pw[k] + ((mask) % pw[k]))
#define debug(a) cerr << #a << " = " << a << " ";
#define debugl(a) cerr << #a << " = " << a << "\n";
const ldb LDINF = 9128739847123.00;
const ldb eps = 1e-9;
const int INF = 1 << 30;
const ldb pi = fabsl(atan2(0.0, -1.0));
using namespace std;


class Range
{
	public:
		int r, l;
		Range(){}
		Range(int l, int r) : l(l), r(r){}
};


inline bool operator < (const Range &r1, const Range & r2)
{
	return r1.r - r1.l > r2.r - r2.l || r1.r - r1.l == r2.r - r2.l && r1.l > r2.l;
}

set < Range > all;
class cmp
{
	public :
		bool operator () (const Range &a, const Range &b)
		{
			return a.r < b.r || a.r == b.r && a.l < b.l;
		}                        
};

set < Range, cmp> oth;
vector <int> quer;
map <int, int > vals;

int fen[500000];
int k[100010];
int l[100010];
int r[100010];
int n, m;

int find(int key)
{
	int l = 0;
	int r = quer.size() - 1;
	int mid, res;
	while (l <= r)
	{
		mid = (l + r) / 2;
		if (quer[mid] == key)
			return mid;
		else if (quer[mid] < key)
			l = mid + 1;
		else r = mid - 1;
	}
	cerr << "BOTVA!";
	return -1;
}


void Add(int x, int k)
{
	x++;
	while (x <= quer.size() + 1)
	{
		fen[x] += k;
		x |= (x + 1);
	}
}

int Sum(int x)
{
	int res = 0;
	while (x > 0)
	{
		res += fen[x];
		x = (x & (x + 1)) - 1;
	}
	return res;
}

int getSum(int l, int r)
{
	l++;
	r++;
	int res = Sum(r);
	if (l - 1 > 0) res -= Sum(l - 1);
	return res;
}

void Load()
{
	cin >> n >> m;
	Range rng;
	rng.r = n + 1;
	rng.l = 1;
	all.insert(rng);
	oth.insert(rng);
	int ll, rr, t;
	int i;
	set <Range> :: iterator it;

	for (i = 0; i < m; i++)
	{
		scanf("%d", &k[i]);
		if (k[i] == 0)
		{
			scanf("%d%d", &l[i], &r[i]);
			quer.push_back(l[i]);
			quer.push_back(r[i]);
		}
		if (k[i])
		{
			if (vals.find(k[i]) == vals.end())
			{
				rng = *all.begin();	
				all.erase(rng);
				oth.erase(rng);
				t = rng.l + (rng.r - rng.l) / 2;
			//	cerr << k[i] << " " << rng.l << " " << rng.r << " " << t << "\n";
				quer.push_back(t);
				if (t - rng.l > 0)
				{
					all.insert(Range(rng.l, t));
					oth.insert(Range(rng.l, t));
				}
				if (rng.r - t - 1 > 0)
				{
					all.insert(Range(t + 1, rng.r));
					oth.insert(Range(t + 1, rng.r));
				}
				vals[k[i]] = t;
			}
			else
			{
				t = vals[k[i]];
			//	cerr << "erasing " << vals[k[i]] << "\n";
				it = oth.lower_bound(Range(-1000, t));
				if (it == oth.end())
				{
					all.insert(Range(t, t + 1));
					oth.insert(Range(t, t + 1));
				}
				else if ((*it).r != t && (*it).l == t + 1)
				{
					ll = t;
					rr = (*it).r;
					oth.erase(it);
					all.erase(Range(ll + 1, rr));
					all.insert(Range(ll, rr));
					oth.insert(Range(ll, rr));
				}
				else if ((*it).r == t)
				{
					
					ll = (*it).l;
					rr = t + 1;
					it++;
					if (it != oth.end() && (*it).l == t + 1)
						rr = (*it).r;
					if (t - ll > 0)
					{
						all.erase(Range(ll, t));
						oth.erase(Range(ll, t));
					}
					if (rr - t - 1 > 0)
					{
						all.erase(Range(t + 1, rr));
						oth.erase(Range(t + 1, rr));
					}
				//	cerr << "adding range " << ll << " " << rr << "\n";
					all.insert(Range(ll, rr));
					oth.insert(Range(ll, rr));
				}
				vals.erase(k[i]);
			}
		}
	}
	sort(quer.begin(), quer.end());
	quer.erase(unique(quer.begin(), quer.end()), quer.end());
	vals.erase(vals.begin(), vals.end());
	all.erase(all.begin(), all.end());
	oth.erase(oth.begin(), oth.end());
	all.insert(Range(1, n + 1));
	oth.insert(Range(1, n + 1));
	for (i = 0; i < m; i++)
	{
		if (k[i] == 0)
		{
			printf("%d\n", getSum(find(l[i]), find(r[i])));
		}
		else 
		{
			if (vals.find(k[i]) == vals.end())
			{
				rng = *all.begin();	
				all.erase(rng);
				oth.erase(rng);
				t = rng.l + (rng.r - rng.l) / 2;
				//cerr << k[i] << " " << rng.l << " " << rng.r << " " << t << "\n";
				if (t - rng.l > 0)
				{
					all.insert(Range(rng.l, t));
					oth.insert(Range(rng.l, t));
				}
				if (rng.r - t - 1 > 0)
				{
					all.insert(Range(t + 1, rng.r));
					oth.insert(Range(t + 1, rng.r));
				}
				vals[k[i]] = t;
				Add(find(t), 1);
			}
			else
			{
				t = vals[k[i]];
			//	cerr << "erasing " << vals[k[i]] << "\n";
				it = oth.lower_bound(Range(-1000, t));
			//	cerr << " it " << (*it).l << " " << (*it).r << "\n";
				if (it == oth.end())
				{
					all.insert(Range(t, t + 1));
					oth.insert(Range(t, t + 1));
				}
				else if ((*it).r != t && (*it).l == t + 1)
				{
					ll = t;
					rr = (*it).r;
					oth.erase(Range(ll + 1, rr));
					all.erase(Range(ll + 1, rr));
					all.insert(Range(ll, rr));
					oth.insert(Range(ll, rr));
				}
				else if ((*it).r == t)
				{
					
					ll = (*it).l;
					rr = t + 1;
					it++;
					if (it != oth.end() && (*it).l == t + 1)
						rr = (*it).r;
					if (t - ll > 0)
					{
						all.erase(Range(ll, t));
						oth.erase(Range(ll, t));
					}
					if (rr - t - 1 > 0)
					{
						all.erase(Range(t + 1, rr));
						oth.erase(Range(t + 1, rr));
					}
				//	cerr << "adding range " << ll << " " << rr << " " << t << "\n";
					all.insert(Range(ll, rr));
					oth.insert(Range(ll, rr));
				}
				Add(find(t), -1);
				vals.erase(k[i]);
			}
		}
	}
}

void Solve()
{

}

#define file "d"
int main()
{
	#ifndef ONLINE_JUDGE
		freopen(file".in", "rt", stdin);
		freopen(file".out", "wt", stdout);
	#endif
	Load();
	Solve();
	return 0;
}
